package web;

import java.util.List;

/**
 * Created by asus on 2017/5/13.
 */
public class Page<T> {
    private  int pageNo;
    private List<T> list;
    private int pageSize;
    private long totalItemNumber;
    public Page(int pageNo){
        this.pageNo=pageNo;
    }

    public int getPageNo() {
        if(pageNo<=0){
            pageNo=1;
        }
        if(pageNo>getTotalPageNumber()){
            pageNo=getTotalPageNumber();
        }
        return pageNo;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize){this.pageSize=pageSize;}

    public void setTotalItemNumber(long totalItemNumber) {
        this.totalItemNumber = totalItemNumber;
    }

    //获取总页数
    public int getTotalPageNumber(){
         int totalPageNumber=(int)totalItemNumber/pageSize;
         if(totalItemNumber%pageSize!=0){
              totalPageNumber++;
         }
         return totalPageNumber;
    }

    public boolean isHasNext(){
        if(getPageNo()<getTotalPageNumber()){
            return true;
        }
        return false;
    }
    public boolean isHasPrev(){
        if(getPageNo()>1){
            return true;
        }
        return false;
    }
    public int getPrevPage(){
        if(isHasPrev()){
            return getPageNo()-1;
        }
        return  getPageNo();
    }
    public int getNextPage(){
        if(isHasNext()){
            return getPageNo()+1;
        }
        return getPageNo();
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNo=" + pageNo +
                ", list=" + list +
                ", pageSize=" + pageSize +
                ", totalItemNumber=" + totalItemNumber +
                '}';
    }
}
